﻿using System;
using System.Net;
using SCG.General;

namespace SCG.SolarVengeanceEngine
{
    public class SVTokenizer : Tokenizer
    {
        //constructors
        public SVTokenizer(SVGame game)
            : base()
        {
            _game = game;
        }
        public SVTokenizer(SVGame game, string source)
            : base(source)
        {
            _game = game;
        }

        //Pluck out a Player
        public Player TokenPlayer
        {
            get
            {
                int idx = TokenInt;
                if (idx == -1)
                    return null;
                else
                    return _game.Players[idx];
            }
        }

        //Pluck out a StarShip type
        public StarShipType TokenStarShipType
        {
            get
            {
                return (StarShipType)TokenInt;
            }
        }

        //Get an Order type token
        public OrderType TokenOrderType
        {
            get
            {
                return (OrderType)TokenInt;
            }
        }

        //get a state change
        public StateChange TokenStateChange
        {
            get
            {
                return (StateChange)TokenInt;
            }
        }

        //Get a StarShip token
        public StarShip TokenStarShip
        {
            get
            {
                int idx = TokenInt;
                if (idx == -1)
                    return null;
                else
                    return _game.LookupMapObject(idx) as StarShip;
            }
        }

        //Get a StarSystem
        public StarSystem TokenStarSystem
        {
            get
            {
                int idx = TokenInt;
                if (idx == -1)
                    return null;
                else
                    return _game.LookupMapObject(idx) as StarSystem;
            }
        }

        //Get a StarShip descriptor
        public StarShipDescriptor TokenStarShipDescriptor
        {
            get
            {
                return new StarShipDescriptor(TokenString);
            }
        }

        //Get a StarShip order instance
        public StarShipOrder TokenStarShipOrder
        {
            get
            {
                string s = TokenString;
                if (s == "-1")
                    return null;
                return StarShipOrder.CreateInstance(_game, s);
            }
        }

        //Get an alliance level token
        public AllianceLevel TokenAllianceLevel
        {
            get
            {            
                return (AllianceLevel)TokenInt;
            }
        }

        //Get an SVMapObject
        public SVMapObject TokenMapObject
        {
            get
            {
                int n = TokenInt;
                return _game.LookupMapObject(n);
            }
        }

        //Get a UDO
        public UDO TokenUDO
        {
            get
            {
                int n = TokenInt;
                if (n == -1)
                    return null;
                else
                    return _game.LookupMapObject(n) as UDO;
            }
        }

        //Get a ChatChannel token
        public ChatChannel TokenChatChannel
        {
            get
            {
                int n = TokenInt;
                return (ChatChannel)n;
            }
        }

        //private members
        private SVGame _game;

        //allow custom object to be tokenized
        protected override string TokenizeObject(object obj)
        {
            if (obj == null)
                return "-1";
            else if (obj is Player)
            {
                Player p = obj as Player;
                return p.Index.ToString();
            }           
            else if (obj is StarShipType)
            {
                StarShipType st = (StarShipType)obj;
                return ((int)st).ToString();
            }
            else if (obj is UDO)
            {
                UDO udo = obj as UDO;
                return udo.ID.ToString();
            }
            else if (obj is OrderType)
            {
                OrderType ot = (OrderType)obj;
                return ((int)ot).ToString();
            }
            else if (obj is StateChange)
            {
                StateChange sc = (StateChange)obj;
                return ((int)sc).ToString();
            }
            else if (obj is StarShip)
            {
                StarShip s = (StarShip)obj;
                return s.ID.ToString();
            }
            else if (obj is StarSystem)
            {
                StarSystem sys = (StarSystem)obj;
                return sys.ID.ToString();
            }
            else if (obj is StarShipDescriptor)
            {
                StarShipDescriptor ssd = obj as StarShipDescriptor;
                return ssd.Persist();
            }
            else if (obj is StarShipOrder)
            {
                StarShipOrder order = obj as StarShipOrder;
                return order.Persist();
            }
            else if (obj is AllianceLevel)
            {
                AllianceLevel al = (AllianceLevel)obj;
                return ((int)al).ToString();
            }
            else if (obj is SVMapObject)
            {
                SVMapObject svm = obj as SVMapObject;
                return svm.ID.ToString();
            }
            else if (obj is ChatChannel)
            {
                ChatChannel cc = (ChatChannel)obj;
                int n = (int)cc;
                return n.ToString();
            }
            else
                return base.TokenizeObject(obj);
        }
    }
}
